<template>
  <!-- 搜索结果 -->
  <div class="search_result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in resultsList"
        :key="index"
        :title="item.title"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from "@/request/home";

export default {
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      resultsList: [],
      loading: false,
      finished: false,
      page: 1,
      per_page: 10
    };
  },
  methods: {
    async onLoad() {
      const { data } = await getSearchResult({
        page: this.page,
        per_page: this.per_page,
        q: this.searchText
      });
      const { results } = data.data;
      this.resultsList.push(...results);
      this.loading = false;
      if (results.length) {
        this.page += 1;
      } else {
        this.finished = false;
      }
    }
  }
};
</script>

<style lang="less" scoped>
.search_result {
  position: fixed;
  left: 0;
  right: 0;
  top: 54px;
  bottom: 0;
  overflow-x: auto;
}
</style>